//
//  DetailCell.m
//  Travel_M_Travel
//
//  Created by dlios on 15-3-27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailCell.h"
#import "UIImageView+WebCache.h"
#import "PlayController.h"
#import "AFNetworking.h"

@interface DetailCell ()
@property (nonatomic, retain) UIImage *kong;
@property (nonatomic, retain) UIImage *shi;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation DetailCell

- (void)dealloc
{
    [_title release];
    [_coverSmall release];
    [_likes release];
    [_playtimes release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.arr = [NSMutableArray array];
        //        self.bigBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        ////        self.bigBtn.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height/2);
        ////        self.bigBtn.backgroundColor = [UIColor redColor];
        //        [self.bigBtn addTarget:self action:@selector(bigBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        //        [self.contentView addSubview:_bigBtn];
        
        self.zan = [UIButton buttonWithType:UIButtonTypeCustom];
        //        zan.frame = CGRectMake(0, self.contentView.frame.size.height/4*3, self.contentView.frame.size.width/3, self.contentView.frame.size.height/4);
        //        [_zan setTitle:@"赞" forState:UIControlStateNormal];
        self.kong = [[UIImage imageNamed:@"zanKong.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.shi = [[UIImage imageNamed:@"zanShi.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [_zan setImage:self.kong forState:UIControlStateNormal];
        [_zan setImage:self.shi forState:UIControlStateSelected];
        [_zan addTarget:self action:@selector(zanAction:) forControlEvents:UIControlEventTouchUpInside];
        //        self.zan.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_zan];
        
        //        self.pl = [UIButton buttonWithType:UIButtonTypeSystem];
        ////        pl.frame = CGRectMake(self.contentView.frame.size.width/3, self.contentView.frame.size.height/4*3, self.contentView.frame.size.width/3, self.contentView.frame.size.height/4);
        //        [_pl setTitle:@"评论" forState:UIControlStateNormal];
        //        [_pl addTarget:self action:@selector(plAction:) forControlEvents:UIControlEventTouchUpInside];
        //        [self.contentView addSubview:_pl];
        
        self.xz = [UIButton buttonWithType:UIButtonTypeCustom];
        //        self.xz.backgroundColor = [UIColor orangeColor];
        [_xz setImage:[[UIImage imageNamed:@"download.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [_xz addTarget:self action:@selector(xzAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_xz];
        
        self.coverSmall = [[UIImageView alloc] init];
        //        _coverSmall.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_coverSmall];
        [_coverSmall release];
        
        self.title = [[UILabel alloc] init];
        self.title.font = [UIFont systemFontOfSize:14];
        [self.title setNumberOfLines:0];
        //        self.title.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_title];
        [_title release];
        
        self.likesImg = [[UIImageView alloc] init];
        //        self.likesImg.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_likesImg];
        [_likesImg release];
        
        self.likes = [[UILabel alloc] init];
        self.likes.font = [UIFont systemFontOfSize:10];
        //        self.likes.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_likes];
        [_likes release];
        
        self.playtimesImg = [[UIImageView alloc] init];
        //        self.playtimesImg.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_playtimesImg];
        [_playtimesImg release];
        
        self.playtimes = [[UILabel alloc] init];
        //        self.playtimes.backgroundColor = [UIColor blueColor];
        self.playtimes.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:_playtimes];
        [_playtimes release];
        
        
    }
    return self;
}

- (void)xzAction:(UIButton *)xz
{
    NSLog(@"!!!!~~%@", self.track.playUrl32);
    
    // 文件路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    NSString *fileName = [NSString stringWithFormat:@"%@/%@.mp3", path, self.track.title];
    
    self.track.localUrl = fileName;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:fileName]) {
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已下载" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [al show];
        [_xz setImage:[UIImage imageNamed:@"downloadFinish.png"] forState:UIControlStateNormal];
    } else {
        NSURL *url = [[NSURL alloc] initWithString:self.track.playUrl32];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.inputStream = [NSInputStream inputStreamWithURL:url];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];
        // 进度
        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            float progress = (float)totalBytesRead/totalBytesExpectedToRead;
            NSLog(@"is download：%f", progress);
            if (progress == 1) {
                UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载已完成" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [al show];
                [_xz setImage:[UIImage imageNamed:@"downloadFinish.png"] forState:UIControlStateNormal];
                self.track.isFinish = YES;
            }
        }];
        [operation start];
    }
    [self.delegate pushViewControllerWithPrama:self.track];
    
    
}

- (void)zanAction:(UIButton *)zan
{
    if (!zan.selected) {
        NSInteger likeNum = [self.track.likes integerValue];
        likeNum += 1;
        self.likes.text = [NSString stringWithFormat:@"%ld", likeNum];
        [_zan setSelected:YES];;
        
    }else if (zan.selected){
        NSInteger likeNum = [self.track.likes integerValue];
        
        self.likes.text = [NSString stringWithFormat:@"%ld", likeNum];
        [_zan setSelected:NO];
        
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //    self.bigBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    //    self.bigBtn.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height*3/4);
    //    [self.bigBtn addTarget:self action:@selector(bigBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.contentView addSubview:_bigBtn];
    
    //    self.bigBtn.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height*2/3);
    self.zan.frame = CGRectMake(0, self.contentView.frame.size.height/4*3, self.contentView.frame.size.width/2, self.contentView.frame.size.height/4);
    //     self.pl.frame = CGRectMake(self.contentView.frame.size.width/3, self.contentView.frame.size.height/4*3, self.contentView.frame.size.width/3, self.contentView.frame.size.height/4);
    self.xz.frame = CGRectMake(self.contentView.frame.size.width/2, self.contentView.frame.size.height/4*3, self.contentView.frame.size.width/2, self.contentView.frame.size.height/4);
    
    // 文件路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    NSString *fileName = [NSString stringWithFormat:@"%@/%@.mp3", path, self.track.title];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:fileName]) {
        [_xz setImage:[[UIImage imageNamed:@"downloadFinish.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        self.track.isFinish = YES;
    }
    // 小图片
    self.coverSmall.frame = CGRectMake(self.contentView.frame.size.width*4/5 - 15, 10, 60, 60);
    NSString *str = self.track.coverSmall;
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    [self.coverSmall setImageWithURL:url];
    
    self.title.frame = CGRectMake(40, 10, self.contentView.frame.size.width/2+30, self.contentView.frame.size.height / 5 * 2);
    self.title.text = self.track.title;
    
    CGFloat x = self.title.frame.origin.x;
    
    self.likesImg.frame = CGRectMake(x, self.title.frame.size.height + 15, 10, 10);
    self.likesImg.image = [UIImage imageNamed:@"likes.png"];
    
    CGFloat y = self.likesImg.frame.origin.y;
    
    self.likes.frame = CGRectMake(x + 10, y, self.contentView.frame.size.width/15, self.contentView.frame.size.height/10);
    NSString *str1 = [NSString stringWithFormat:@"%@", self.track.likes];
    self.likes.text = str1;
    
    self.playtimesImg.frame = CGRectMake(x + self.likes.frame.size.width + 20, y, 10, 10);
    self.playtimesImg.image = [UIImage imageNamed:@"playtimes.png"];
    
    self.playtimes.frame = CGRectMake(self.likes.frame.origin.x + self.likes.frame.size.width + 20, y, self.contentView.frame.size.width/10 * 2, self.contentView.frame.size.height/10);
    NSString *str2 = [NSString stringWithFormat:@"%@", self.track.playtimes];
    self.playtimes.text = str2;
}


//- (void)bigBtnAction:(UIButton *)button
//{
////    PlayController *play = [[PlayController alloc] init];
////    [self.detail.navigationController pushViewController:play animated:NO];
////    [play release];
//    [self.delegate pushViewController];
//
//
//}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
